import request from './request'

// 订单接口类型定义
export interface OrderItem {
  id?: number
  productId: number
  productName: string
  productImage?: string
  price: number
  quantity: number
  subtotal: number
}

export interface Order {
  id?: number
  orderNumber: string
  userId: number
  status: 'PENDING' | 'PAID' | 'SHIPPED' | 'DELIVERED' | 'COMPLETED' | 'CANCELLED'
  totalAmount: number
  receiverName: string
  receiverPhone: string
  receiverAddress: string
  paymentMethod: 'ALIPAY' | 'WECHAT' | 'BANK_CARD' | 'CASH'
  paymentStatus: 'UNPAID' | 'PAID' | 'REFUNDED'
  remark?: string
  orderItems?: OrderItem[]
  createdAt?: string
  updatedAt?: string
}

// 获取用户订单列表
export const getUserOrders = (userId: number, params?: {
  page?: number
  size?: number
  status?: string
}) => {
  return request({
    url: '/orders',
    method: 'get',
    params: { userId, ...params }
  })
}

// 获取订单详情
export const getOrderDetail = (userId: number, orderId: number) => {
  return request({
    url: `/orders/${orderId}`,
    method: 'get',
    params: { userId }
  })
}

// 创建订单
export const createOrder = (userId: number, orderData: {
  totalAmount: number
  receiverName: string
  receiverPhone: string
  receiverAddress: string
  paymentMethod: string
  remark?: string
  items: OrderItem[]
}) => {
  return request({
    url: '/orders',
    method: 'post',
    params: { userId },
    data: orderData
  })
}

// 取消订单
export const cancelOrder = (userId: number, orderId: number) => {
  return request({
    url: `/orders/${orderId}/cancel`,
    method: 'put',
    params: { userId }
  })
}